//
//  GCMBZMXViewController.m
//  GeeCityManager
//
//  Created by 聚城科技 on 16/8/9.
//  Copyright © 2016年 聚城科技. All rights reserved.
//

#import "GCMBZMXViewController.h"
#import "GCMQualityGZRHRequst.h"
#import "GCMQualityRequst.h"

@interface GCMBZMXViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>


@end

@implementation GCMBZMXViewController{
    
    NSMutableArray *_datasource;
    NSMutableArray *_dataSource;
    UITableView *_tableV;
    UITextField *_morningScoreF;
    UITextField *_afternongScorceF;
    UITextField *_ScorceF;
    UITextField *_beizhuF;
    CGSize labelsize;
    
}
-(void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBarHidden = NO;
    [self downLoadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
    [self createUI];
    [self createTableView];
    [self downLoadData];
}

-(void)createUI{

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2, 64, SCREEN_WIDTH / 2 , 40)];
    label.text = @"当前总分数";
    label.textColor = [UIColor lightGrayColor];
    label.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1.0];
    label.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:label];
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH/2, 40)];
    if (!self.fen) {
        label1.text = @"  0";
    }else{
        label1.text = self.fen;
    }
    label1.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1.0];
    label1.textColor = [UIColor lightGrayColor];
    [self.view addSubview:label1];
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 50, SCREEN_WIDTH, 50)];
    [button setTitle:@"提交" forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor colorWithRed:0.24 green:0.43 blue:0.64 alpha:1.00]];
    [button addTarget:self action:@selector(onbuttonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}

-(void)createTableView{
    
    _tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 64 + 40, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 50 - 40 )];
    _tableV.delegate = self;
    _tableV.dataSource =self;
    _tableV.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.0];
    [self.view addSubview:_tableV];
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

    UILabel * textLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 10, self.view.frame.size.width -10 , labelsize.height+50)];
    textLabel.font = [UIFont systemFontOfSize:15];
    textLabel.text = self.str;
    [textLabel setNumberOfLines:0];
    textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    textLabel.textColor = [UIColor lightGrayColor];
    [cell.contentView addSubview:textLabel];
//    cell.contentView.backgroundColor = [UIColor blueColor];
    return cell;
}







//  通过textfield的代理函数来控制变化
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
{  //string就是此时输入的那个字符textField就1输入框返回YES就是可以改变输入框的值NO相反
    
    if ([string isEqualToString:@"\n"])  //按会车可以改变
    {
        return YES;
    }
    
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容
    
    if (_ScorceF == textField)  //判断是否时我们想要限定的那个输入框
    {
        if ([toBeString intValue] > [self.fen intValue]) { //如果输入框内容大于20则弹出警告
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"不能超过最大打分数" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];
            return NO;
        }
    }
    if (_morningScoreF == textField)  //判断是否时我们想要限定的那个输入框
    {
        if ([toBeString intValue] > [self.fen intValue]) { //如果输入框内容大于20则弹出警告
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"不能超过上午最大打分数" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];
            return NO;
        }
    }
    if (_afternongScorceF == textField)  //判断是否时我们想要限定的那个输入框
    {
        if ([toBeString intValue] > [self.fen intValue]) { //如果输入框内容大于20则弹出警告
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"不能超过下午最大打分数" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];
            return NO;
        }
    }
    return YES;
}





-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    UIView *viewFooter = [[UIView alloc]init];
//    viewFooter.backgroundColor = [UIColor yellowColor];
    
    if ([self.once isEqualToString:@"一天一次"]) {
        UILabel *label  = [[UILabel alloc]initWithFrame:CGRectMake( 0, 0, SCREEN_WIDTH, 30)];
        label.text = @"打分";
        label.textAlignment = NSTextAlignmentCenter;
        label.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1.0];
        [viewFooter addSubview:label];
        _ScorceF = [[UITextField alloc]initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH-1, 30)];
        _ScorceF.delegate = self;
        _ScorceF.backgroundColor = [UIColor whiteColor];
        _ScorceF.keyboardType = UIKeyboardTypeNumberPad;
        for (NSDictionary *dic in _datasource) {
            _ScorceF.text = [NSString stringWithFormat:@"%@",[dic valueForKey:@"num"]];
            
        }
        [viewFooter addSubview:_ScorceF];

    }else{
    
        NSArray * array = @[@"    上午",@"    下午"];
        for (int i = 0; i < 2; i++) {
            UILabel *label  = [[UILabel alloc]initWithFrame:CGRectMake(1*i +(i *(SCREEN_WIDTH-1)/2), 0, (SCREEN_WIDTH-1)/2, 30)];
            label.text = array[i];
            label.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1.0];
            [viewFooter addSubview:label];
        }
        //    上午得分         //    下午得分
        _morningScoreF = [[UITextField alloc]initWithFrame:CGRectMake(0, 30, (SCREEN_WIDTH-1)/2, 30)];
        _morningScoreF.backgroundColor = [UIColor whiteColor];
        _morningScoreF.keyboardType = UIKeyboardTypeNumberPad;
        _morningScoreF.delegate = self;

        _afternongScorceF = [[UITextField alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-1)/2 +1, 30, (SCREEN_WIDTH-1)/2, 30)];
        _afternongScorceF.backgroundColor = [UIColor whiteColor];
        _afternongScorceF.keyboardType = UIKeyboardTypeNumberPad;
        _afternongScorceF.delegate = self;

        for (NSDictionary *dic in _datasource) {
            if ([[dic valueForKey:@"shijian"] isEqualToString:@"上午"]) {
                _morningScoreF.text = [NSString stringWithFormat:@"%@",[dic valueForKey:@"num"]];
            }else if([[dic valueForKey:@"shijian"] isEqualToString:@"下午"]){
                _afternongScorceF.text = [NSString stringWithFormat:@"%@",[dic valueForKey:@"num"]];
            }
        }
        [viewFooter addSubview:_morningScoreF];
        [viewFooter addSubview:_afternongScorceF];

    }
    
    //    备注
    UILabel *labelB  = [[UILabel alloc]initWithFrame:CGRectMake(0, 60, SCREEN_WIDTH, 30)];
    labelB.text = @"备注";
    labelB.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1.0];
    labelB.textAlignment = NSTextAlignmentCenter;
    [viewFooter addSubview:labelB];
    //    备注输入框
    _beizhuF = [[UITextField alloc]initWithFrame:CGRectMake(0, 90, SCREEN_WIDTH-1, 30)];
    _beizhuF.backgroundColor = [UIColor whiteColor];
    for (NSDictionary *dic in _datasource) {
        _beizhuF.text = [NSString stringWithFormat:@"    %@",[dic valueForKey:@"t_text"]];
//        NSLog(@"%@",[dic valueForKey:@"t_text"]);
    }
    [viewFooter addSubview:_beizhuF];
    return viewFooter;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 120;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    return labelsize.height+70;

}

-(void)onbuttonClick{
    
    NSMutableDictionary *parameters = [[NSMutableDictionary alloc]init];
    parameters[@"a_id"] = self.a_id;
    parameters[@"yiji_id"] = self.yiji_id;
    parameters[@"date"] = self.hz_date;
    parameters[@"sj_id"] = self.bj_id;
    parameters[@"type"] = self.once;
    parameters[@"ywy"] = [GCMUserInfo instance].uid;
    if ([self.once isEqualToString:@"一天二次"]) {
        parameters[@"AM_num"] = _morningScoreF.text;
        parameters[@"PM_num"] = _afternongScorceF.text;
        parameters[@"yici_num"] = @"";
    }else{
        parameters[@"AM_num"] = @"";
        parameters[@"PM_num"] = @"";
        parameters[@"yici_num"] = _ScorceF.text;
    }
    parameters[@"t_text"] = _beizhuF.text;
//    NSLog(@"测试数据%@",parameters);
    
    [GCMQualityRequst postDataWithParmerts:parameters WithUrl:QUALISTDAFEN_URL Success:^(id obj, NSError *err) {

        UIAlertView * alertview = [[UIAlertView alloc] initWithTitle:@"确定打分？" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alertview.delegate = self;
        [alertview show];
        
    }];
    
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}


-(void)downLoadData{
    
        [GCMQualityGZRHRequst postQGZRHDetailDataWithaId:self.a_id WithHtDate:self.hz_date WithBianji:self.bj_id Success:^(id obj, NSError *err) {
            
            _datasource  = obj;
//            NSLog(@"hahahaha%@",_datasource);
            [_tableV reloadData];
        }];
}

-(void)initData{
    
    _datasource = [[NSMutableArray alloc]init];
    _dataSource = [[NSMutableArray alloc]init];
    [_dataSource addObject:self.str];
//    根据文字的大小和数量自定义label的高度的代码
    CGSize size = CGSizeMake(self.view.frame.size.width - 10,CGFLOAT_MAX);
    UIFont* theFont = [UIFont systemFontOfSize:15];
    
    //计算文字所占区域
    labelsize = [self.str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : theFont} context:nil].size;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
